/*
 * Copyright 2008 Daniel Freitas
 *
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Army Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Army Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package cheesr.application;

import java.text.NumberFormat;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;

import cheesr.components.ShoppingCartPanel;
import cheesr.model.Cheese;
import cheesr.util.PropertyComparator;
import cheesr.util.SortOrder;

public class Index extends CheesrPage {

    private SortOrder sortOrder = SortOrder.ASCENDING;
    private String sortingBy = "name";
    private NumberFormat currencyNumberFormat = NumberFormat.getCurrencyInstance();
    private AjaxPagingNavigator topPagingNavigator;
    private AjaxPagingNavigator bottomPagingNavigator;
    private ShoppingCartPanel shoppingCartPanel;

    @SuppressWarnings("serial")
    private Link checkOutLink = new Link("checkout") {
        @Override
        public void onClick() {
            setResponsePage(new CheckOutPage());
        }

        @Override
        public boolean isVisible() {
            return getCart().getCheeses().isEmpty() == false;
        }
    };

    @SuppressWarnings("serial")
    private PageableListView cheeses = new PageableListView("cheeses", getCheeses(new PropertyComparator<Cheese>(
            "name", sortOrder)), 5) {
        @Override
        protected void populateItem(ListItem item) {
            Cheese cheese = (Cheese) item.getModelObject();
            item.add(new Label("name", cheese.getName()));
            item.add(new Label("description", cheese.getDescription()));
            item.add(new Label("price", currencyNumberFormat.format(cheese.getPrice())));
            item.add(new AjaxFallbackLink("add", item.getModel()) {

                @Override
                public void onClick(AjaxRequestTarget target) {
                    Cheese selected = (Cheese) getModelObject();
                    getCart().add(selected);

                    if (target != null) {
                        target.addComponent(shoppingCartPanel);
                        target.addComponent(checkOutLink);
                    }
                }
            });
        }
    };

    @SuppressWarnings("serial")
    public Index() {
        shoppingCartPanel = new ShoppingCartPanel("shoppingcart", getCart());
        final WebMarkupContainer listOfCheeses = new WebMarkupContainer("listOfCheeses");
        listOfCheeses.add(cheeses);
        listOfCheeses.setOutputMarkupId(true);
        add(listOfCheeses);
        topPagingNavigator = new AjaxPagingNavigator("topNavigator", cheeses) {

            @Override
            protected void onAjaxEvent(AjaxRequestTarget target) {
                if (target != null) {
                    target.addComponent(bottomPagingNavigator);
                }

                super.onAjaxEvent(target);
            }
        };
        bottomPagingNavigator = new AjaxPagingNavigator("bottomNavigator", cheeses) {
            @Override
            protected void onAjaxEvent(AjaxRequestTarget target) {
                if (target != null) {
                    target.addComponent(topPagingNavigator);
                }

                super.onAjaxEvent(target);
            }
        };
        add(topPagingNavigator);
        add(bottomPagingNavigator);

        add(new AjaxFallbackLink("sortByNameLink") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                if (sortingBy.equals("name")) {
                    sortOrder = sortOrder.getOpposite();
                } else {
                    sortOrder = SortOrder.ASCENDING;
                    sortingBy = "name";
                }

                cheeses.setModelObject(getCheeses(new PropertyComparator<Cheese>("name", sortOrder)));

                if (target != null) {
                    target.addComponent(listOfCheeses);
                }
            }
        });
        add(new AjaxFallbackLink("sortByPriceLink") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                if (sortingBy.equals("price")) {
                    sortOrder = sortOrder.getOpposite();
                } else {
                    sortOrder = SortOrder.ASCENDING;
                    sortingBy = "price";
                }

                cheeses.setModelObject(getCheeses(new PropertyComparator<Cheese>("price", sortOrder)));

                if (target != null) {
                    target.addComponent(listOfCheeses);
                }
            }
        });

        add(shoppingCartPanel);
        checkOutLink.setOutputMarkupId(true);
        checkOutLink.setOutputMarkupPlaceholderTag(true);
        add(checkOutLink);
    }
}